package com.czwj.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.czwj.dao.BudgetMainMapper;
import com.czwj.dao.Duty2Mapper;
import com.czwj.dao.Duty3Mapper;
import com.czwj.dao.Duty4Mapper;
import com.czwj.dao.ProjectItemChildMapper;
import com.czwj.dao.ProjectItemMapper;
import com.czwj.entities.BudgetMain;
import com.czwj.entities.Duty2;
import com.czwj.entities.Duty3;
import com.czwj.entities.Duty4;
import com.czwj.entities.ProjectItem;
import com.czwj.service.ProjectItemService;

@Service
public class ProjectItemServiceImpl implements ProjectItemService{
	@Resource
	ProjectItemMapper mapper;
	
	@Resource
	Duty2Mapper mapper2;
	
	@Resource
	Duty3Mapper mapper3;
	
	@Resource
	Duty4Mapper mapper4;
	
	@Resource
	BudgetMainMapper bmapper;
	
	@Override
	public List<ProjectItem> searchforlevel1(Map<String,Object> Params){
		// TODO Auto-generated method stub
		return this.mapper.searchforlevel1(Params);
	}
	
	@Override
	public int countforlevel1(Map<String,Object> Params){
		// TODO Auto-generated method stub
		return this.mapper.countforlevel1(Params);
	}
	
	@Transactional
	@Override
	public int update(ProjectItem item){
		// TODO Auto-generated method stub
		mapper2.deleteByProjectitemid(item.getId());
		mapper3.deleteByProjectitemid(item.getId());
		mapper4.deleteByProjectitemid(item.getId());
		int cnt = 0;
		if(item.getDuty2() != null){
			for(int d2:item.getDuty2()){
				cnt += mapper2.insertSelective(new Duty2(item.getId(),d2));
			}
		}
		if(item.getDuty3() != null){
			for(int d3:item.getDuty3()){
				cnt += mapper3.insertSelective(new Duty3(item.getId(),d3));
			}
		}
		if(item.getDuty4() != null){
			for(int d4:item.getDuty4()){
				cnt += mapper4.insertSelective(new Duty4(item.getId(),d4));
			}
		}
		BudgetMain budgetMain = bmapper.searchbyprojectitemid(item.getId());
		budgetMain.setMoney(item.getCompanymoney());
		cnt += bmapper.updateByPrimaryKeySelective(budgetMain);
		cnt += this.mapper.updateByPrimaryKeySelective(item);
		return cnt;
	}
	
	@Transactional
	@Override
	public int insert(ProjectItem item){
		// TODO Auto-generated method stub
		int cnt = 0;
		cnt += this.mapper.insertSelective(item);
		ProjectItem pItem = this.mapper.findByProjectNo(item.getProjectno());
		for(int d2:item.getDuty2()){
			cnt += mapper2.insertSelective(new Duty2(pItem.getId(),d2));
		}
		for(int d3:item.getDuty3()){
			cnt += mapper3.insertSelective(new Duty3(pItem.getId(),d3));
		}
		for(int d4:item.getDuty4()){
			cnt += mapper4.insertSelective(new Duty4(pItem.getId(),d4));
		}
		BudgetMain budgetMain = new BudgetMain();
		budgetMain.setProjectitemid(pItem.getId());
		budgetMain.setStatus(2);
		budgetMain.setMoney(item.getCompanymoney());
		budgetMain.setCreatedate(new Date());
		budgetMain.setCreateip(item.getCreateip());
		budgetMain.setCreaterid(item.getCreaterid());
		cnt += bmapper.insertSelective(budgetMain);
		return cnt;
	}
	
	@Override
	public List<ProjectItem> searchforlevel2(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.searchforlevel2(map);
	}
	
	@Override
	public int countforlevel2(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.countforlevel2(map);
	}
	
	@Override
	public List<ProjectItem> searchforlevel3(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.searchforlevel3(map);
	}
	
	@Override
	public int countforlevel3(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.countforlevel3(map);
	}
	
	@Override
	public ProjectItem findByProjectNo(String projectno){
		// TODO Auto-generated method stub
		return this.mapper.findByProjectNo(projectno);
	}
	
	@Override
	public ProjectItem selectByPrimaryKey(int id){
		// TODO Auto-generated method stub
		return this.mapper.selectByPrimaryKey(id);
	}
	
	@Override
	public int updateByPrimaryKeySelective(ProjectItem item){
		// TODO Auto-generated method stub
		return this.mapper.updateByPrimaryKeySelective(item);
	}
	
	@Override
	public List<ProjectItem> searchforlevel0(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.searchforlevel0(map);
	}
	
	@Override
	public int countforlevel0(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.countforlevel0(map);
	}
	
	@Override
	public List<ProjectItem> searchtypeforlevel0(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.searchtypeforlevel0(map);
	}
	
	@Override
	public List<ProjectItem> searchtypeforlevel1(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.searchtypeforlevel1(map);
	}
	
	@Override
	public List<ProjectItem> searchtypeforlevel2(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.searchtypeforlevel2(map);
	}
	
	@Override
	public List<ProjectItem> searchtypeforlevel3(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.searchtypeforlevel3(map);
	}
	
	@Override
	public List<ProjectItem> projectitemswithoutfinishapply1(Integer id){
		// TODO Auto-generated method stub
		return this.mapper.projectitemswithoutfinishapply1(id);
	}
	
	@Override
	public List<ProjectItem> projectitemswithoutfinishapply2(Integer id){
		// TODO Auto-generated method stub
		return this.mapper.projectitemswithoutfinishapply2(id);
	}
	
	@Override
	public List<ProjectItem> projectitemswithoutfinishapply3(Integer id){
		// TODO Auto-generated method stub
		return this.mapper.projectitemswithoutfinishapply3(id);
	}
	
	@Override
	public List<ProjectItem> projectitemswithoutfinishapply4(Integer id){
		// TODO Auto-generated method stub
		return this.mapper.projectitemswithoutfinishapply4(id);
	}
	
	/** 
	* 
	* @param name
	* @return
	* @see com.czwj.service.ProjectItemService#selectByName(java.lang.String)
	*/
	@Override
	public ProjectItem selectByName(String name){
		// TODO Auto-generated method stub
		return this.mapper.selectByName(name);
	}
	
	/** 
	* 
	* @param id
	* @return
	* @see com.czwj.service.ProjectItemService#searchByCreater(java.lang.Integer)
	*/
	@Override
	public List<ProjectItem> searchByCreater(Integer id){
		// TODO Auto-generated method stub
		return this.mapper.searchByCreater(id);
	}
	
	/** 
	* 
	* @return
	* @see com.czwj.service.ProjectItemService#searchByCompany()
	*/
	@Override
	public List<ProjectItem> searchByCompany(int companyid){
		// TODO Auto-generated method stub
		return this.mapper.searchByCompany(companyid);
	}
	
	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.ProjectItemService#updatestatus(java.util.Map)
	*/
	@Override
	public int updatestatus(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.updatestatus(map);
	}

	/** 
	* 
	* @return
	* @see com.czwj.service.ProjectItemService#search()
	*/
	@Override
	public List<ProjectItem> search(){
		// TODO Auto-generated method stub
		return this.mapper.search();
	}
}
